﻿using Batman.Maintenance.DTO;
using Batman.Maintenance.DTO.ActionBar;
using Batman.Maintenance.DTO.CreateForm;
using Batman.Maintenance.Utils;
using System;
using System.Linq.Expressions;

namespace Batman.Maintenance
{
    public interface IMaintenanceModel
    {
        string Id { get; }

        Type GetEntityType();
    }

    public interface IMaintenanceModel<T> : IMaintenanceModel where T : class
    {
        MaintenanceMainTable<T> MainTable { get; set; }
    }

    public class MaintenanceModel<T> : IMaintenanceModel<T> where T : class
    {
        /// <summary>
        /// 主键字段
        /// </summary>
        public string PrimaryKey { get; set; }

        public MaintenanceMainTable<T> MainTable { get; set; }

        public MaintenanceSearchBar<T> SearchBar { get; set; }

        public MaintenceActionBar ActionBar { get; set; }

        public MaintenanceCreateForm<T> CreateForm { get; set; }

        public MaintenanceUpdateForm<T> UpdateForm { get; set; }


        public string Id { get { return typeof(T).Name; } }

        private Type _entityType;

        public MaintenanceModel()
        {
            MainTable = new MaintenanceMainTable<T>();
            SearchBar = new MaintenanceSearchBar<T>();
            ActionBar = new MaintenceActionBar();
            CreateForm = new MaintenanceCreateForm<T>();
            UpdateForm = new MaintenanceUpdateForm<T>();
            _entityType = typeof(T);
        }

        public Type GetEntityType()
        {
            return _entityType;
        }

        /// <summary>
        /// 设置主键字段
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="property"></param>
        public void HasKey<TResult>(Expression<Func<T, TResult>> property)
        {
            this.PrimaryKey = ExpressionUtil.GetSmallHumpWord(property);
        }
    }
}
